<?php

function page() {
	$handle = file('./page.txt',1);
	//file把文件读入一个数组，参数为文件名，成功返回数组，失败返回false
	$count = count($handle);
	//count 返回数组元素的个数
	$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
	//isset判断变量是否被设置，intval返回一个int值
	if ($page<1 || $page>$count) $page = 1;
	$pnum = 5;
	$begin = ($page - 1) * $pnum;
	$end = ($begin + $pnum) > $count ? $count : $begin + $pnum;
	for ($i = $begin; $i < $end; $i++) { 
		echo '<strong>'.($i+1).'</strong>'.rtrim($handle[$i]).'<br>';
	}
	for ($i=1; $i <= ceil($count / $pnum); $i++) {
		if ($i==$_GET['page']) {
			echo "<a href=?page={$i} style='color:red;'>{$i}</a>";
			continue;
		} 
		echo "<a href=?page={$i}>{$i}</a>";
	}
}
//page();

?>
<?php
function send_post($url, $postdata) {
	$content = http_build_query($postdata);
	$options = array(
		'http' => array(
			'method' => 'POST',
			'header' => 'Content-type:application/x-www-form-urlencoded',//请求的与实体对应的MIME信息
			'content' => $content,
			'timeout' => 15 * 60 // 超时时间（单位:s）
		)
	);
	$context = stream_context_create($options);
	file_put_contents('D:/send_post.txt', date('Y-m-d H:i:s').json_encode($postdata), FILE_APPEND);
	return file_get_contents($url, false, $context);
}
function cross_request_header_set() {//设置跨域头部
	header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Cookie, Accept');
    header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, OPTIONS');
    header('Access-Control-Allow-Credentials: true');
}
$url = 'http://lara.com/post';
$postdata = ['username'=>'xwl'];
//echo send($url, $postdata);
?>
<?php
function send_get($url, $postdata) {
	$content = http_build_query($postdata);
	$options = array(
		'http'=>array(
			'method'=>"GET",
			'header' => 'Content-type:application/x-www-form-urlencoded',//请求的与实体对应的MIME信息
			'timeout' => 15 * 60 // 超时时间（单位:s）
		)
	);
	$context = stream_context_create($options);
	file_put_contents('D:/send_post.txt', date('Y-m-d H:i:s').json_encode($postdata), FILE_APPEND);
	return file_get_contents($url.'?'.$content, false, $context);
}
$url = 'http://lara.com';
$postdata = ['username'=>'1173331414@qq.com'];
//echo send_get($url, $postdata);
?>


